﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class PeakSetting
    {
        public static bool Add(Model.PeakSetting value)
        {
            string condition = @"INSERT INTO [TablePeakSetting] ([LiftNum], [Weekday], [TimeFrom], [TimeTo]) 
                                VALUES (@LiftNum, @Weekday, @TimeFrom, @TimeTo)";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(value));
            if (ret > 0) return true;
            return false;
        }

        public static bool Update(Model.PeakSetting value)
        {
            string condition = @"UPDATE [TablePeakSetting] SET 
                                    [LiftNum] = @LiftNum, 
                                    [Weekday] = @Weekday, 
                                    [TimeFrom] = @TimeFrom, 
                                    [TimeTo] = @TimeTo 
                                WHERE (([ID] = @ID))";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(value));
            if (ret > 0) return true;
            return false;
        }

        public static bool Delete(int id)
        {
            return Global.Delete("TablePeakSetting", "ID", id.ToString());
        }

        private static OleDbParameter[] Param(Model.PeakSetting model)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@LiftNum", model.LiftNum));
            param.Add(new OleDbParameter("@Weekday", model.Weekday));
            param.Add(new OleDbParameter("@TimeFrom", model.TimeFrom));
            param.Add(new OleDbParameter("@TimeTo", model.TimeTo));
            param.Add(new OleDbParameter("@ID", model.ID));

            return param.ToArray();
        }

        public static bool Exist(int id)
        {
            return Global.Exist("TablePeakSetting", "ID", id.ToString());
        }

        public static List<Model.PeakSetting> List(string condition)
        {
            return Deserialize(Global.List("TablePeakSetting", condition));
        }

        private static List<Model.PeakSetting> Deserialize(DataTable dt)
        {
            List<Model.PeakSetting> list = null;
            if (null != dt && dt.Rows.Count > 0)
            {
                list = new List<Model.PeakSetting>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(Deserialize(r));
                }
            }
            return list;
        }

        private static Model.PeakSetting Deserialize(DataRow r)
        {
            Model.PeakSetting model = null;
            if (null != r)
            {
                model = new Model.PeakSetting();
                model.ID = r["ID"].ToString().toInt32();
                model.LiftNum = r["LiftNum"].ToString().toInt32();
                model.Weekday = r["Weekday"].ToString().toInt32();
                model.TimeFrom = r["TimeFrom"].ToString();
                model.TimeTo = r["TimeTo"].ToString();
            }
            return model;
        }
    }
}
